#include "stdafx.h"
#include "ArtSplitterWnd.h"
#include "ArtSplitterCell.h"

// CArtSplitterWnd

STDMETHODIMP CArtSplitterWnd::InterfaceSupportsErrorInfo(REFIID riid)
{
	static const IID* arr[] = 
	{
		&IID_IArtSplitterWnd
	};

	for (int i=0; i < sizeof(arr) / sizeof(arr[0]); i++)
	{
		if (InlineIsEqualGUID(*arr[i],riid))
			return S_OK;
	}

	return S_FALSE;
}


//CArtSplitterCell* CArtSplitterWnd::CreateSplitterCell()
//{
//	CArtSplitterCell_	 *pCell = NULL;
//
//	CArtSplitterCell_::CreateInstance(&pCell);
//	pCell->AddRef();
////	pCell->SetID(m_SplitterCells.GetCount());
//
//	m_SplitterCells.AddTail(static_cast<CArtSplitterCell*>(pCell));
//
//	return static_cast<CArtSplitterCell*>(pCell);
//}


HRESULT CArtSplitterWnd::CreateSplitterCell(CArtSplitterCell **_pSplitterCell)
{
	CComObject<CArtSplitterCell>	*pSplitterCell;
	HRESULT							hr = S_FALSE;

	ATLASSERT(NULL != _pSplitterCell);

	hr = CComObject<CArtSplitterCell>::CreateInstance(&pSplitterCell);
	COM_ASSERT(hr);
	if (S_OK == hr)
	{
		*_pSplitterCell = pSplitterCell;
		pSplitterCell->AddRef();
		pSplitterCell->SetID(m_CurID++);
		this->m_SplitterCells.AddTail(*_pSplitterCell);
	}

	return hr;
}


STDMETHODIMP CArtSplitterWnd::CreateSplitterWnd(HWND hWnd,
												long nLeft,
												long nTop,
												long nRight,
												long nButtom,
												IArtSplitterCell **ppSplitterItem)
{
	DWORD dwStyle	= 0;
	DWORD dwExStyle = 0;
	HRESULT hr = S_OK;

	if (::IsWindow(hWnd))
	{
		dwStyle = WS_CHILD | WS_VISIBLE;
		this->Create(hWnd, CRect(nLeft, nTop, nRight, nButtom), NULL, dwStyle, dwExStyle);

		hr = this->CreateSplitterCell(&m_pMainCell);
		COM_ASSERT(hr);
		if (S_OK == hr)
		{
			CRect	_Rect(nLeft, nTop, nRight, nButtom);
			m_pMainCell->SetRect(&_Rect);
			m_pMainCell->SetSplitterWnd(this);
			if (ppSplitterItem != NULL)
			{
				m_pMainCell->QueryInterface(IID_IArtSplitterCell, (void**)ppSplitterItem);
			}
		}
	}

	return hr;
}


STDMETHODIMP CArtSplitterWnd::MoveWnd(long nLeft, long nTop, long nRight, long nButtom)
{
	::MoveWindow(this->m_hWnd, nLeft, nTop, nRight, nButtom, TRUE);

	this->UpdateLayout();

	return S_OK;
}


STDMETHODIMP CArtSplitterWnd::get_SplitterWidth(LONG* pVal)
{
	*pVal = this->m_uiSplitterWidth;

	return S_OK;
}


STDMETHODIMP CArtSplitterWnd::put_SplitterWidth(LONG newVal)
{
	if (this->m_uiSplitterWidth != newVal)
	{
		this->m_uiSplitterWidth = newVal;
		this->UpdateLayout();
	}

	return S_OK;
}


STDMETHODIMP CArtSplitterWnd::UpdateLayout(void)
{
	HRESULT hr = S_FALSE;

	try
	{
		if (NULL != m_pMainCell)
		{
			m_pMainCell->UpdateLayout();
		}
	}
	catch (...)
	{
		return S_FALSE;
	}

	return S_OK;
}


LRESULT CArtSplitterWnd::OnEraseBkgnd(HDC hdc)
{

	return TRUE; // background is erased
}


STDMETHODIMP CArtSplitterWnd::get_MainSplitterCell(IArtSplitterCell** pVal)
{
	HRESULT			hr	= S_FALSE;

	try
	{
		if (NULL != m_pMainCell)
		{
			hr = m_pMainCell->QueryInterface(IID_IArtSplitterCell, (void**)pVal);
		}
	}
	catch (...)
	{
		return S_FALSE;
	}

	return hr;
}


STDMETHODIMP CArtSplitterWnd::get_ShowDragging(VARIANT_BOOL* pVal)
{
	// TODO: Add your implementation code here
	*pVal = this->m_bShowDragging;

	return S_OK;
}


STDMETHODIMP CArtSplitterWnd::put_ShowDragging(VARIANT_BOOL newVal)
{
	this->m_bShowDragging = newVal;

	return S_OK;
}


LRESULT CArtSplitterWnd::OnSize(UINT state, CSize Size)
{
	CRect rect(0, 0, Size.cx, Size.cy);

	if (NULL != m_pMainCell)
	{
		this->m_pMainCell->SetRect(&rect);
		this->m_pMainCell->UpdateLayout();
	}

	return 0;
}

LRESULT CArtSplitterWnd::OnKillFocus(HWND hwndNewFocus)
{

	return 0;
}


LRESULT CArtSplitterWnd::OnMouseActivate(HWND wndTopLevel, UINT nHitTest, UINT message)
{
	POSITION					pos = NULL;
	CArtSplitterCell			*pCell = NULL;
	CComQIPtr<IGenericPanel>	pGenericPanel;
	HWND						hWndCtrl;
	POINT						pt = {0};

	::GetCursorPos(&pt);
	::MapWindowPoints(NULL, this->m_hWnd, &pt, 1);

	pos = m_SplitterCells.GetHeadPosition();
	m_SplitterCells.GetNext(pos); // 
	while (pos != NULL)
	{
		pCell = (CArtSplitterCell*)m_SplitterCells.GetNext(pos);
		if (pCell->GetRect().PtInRect(pt))
		{
			m_pActiveCell = pCell;
			break;
		}

		pGenericPanel.Release();
	}

	return 0;
}


LRESULT CArtSplitterWnd::OnSetFocus(HWND hwndOldFocus)
{
	return 0;
}


STDMETHODIMP CArtSplitterWnd::SetActiveCell(VARIANT varActiveCell)
{
	return S_OK;
}


STDMETHODIMP CArtSplitterWnd::get_ActiveCell(IArtSplitterCell** ppActiveCell)
{
	if (m_pActiveCell != NULL)
	{
		return m_pActiveCell->QueryInterface(IID_IArtSplitterCell, (void**)ppActiveCell);
	}

	return E_INVALIDARG;
}


STDMETHODIMP CArtSplitterWnd::AddNotifyCommand(INotifyCommand* pNotifyCommand)
{
#ifdef DEBUG
	CString strItem;
	this->GetWindowText(strItem);
	ATLTRACE(_T("CGenericPanel::AddNotifyCommand text window is :: %s\n"), (LPCTSTR)strItem);
#endif

	return CNotifyCommands::Add(pNotifyCommand);
}


STDMETHODIMP CArtSplitterWnd::RemoveNotifyCommand(INotifyCommand* pNotifyCommand)
{
#ifdef DEBUG
	CString strItem;
	this->GetWindowText(strItem);
	ATLTRACE(_T("CGenericPanel::RemoveNotifyCommand text window is :: %s\n"), (LPCTSTR)strItem);
#endif

	return CNotifyCommands::Remove(pNotifyCommand);
}


STDMETHODIMP CArtSplitterWnd::get_NotifyCommands(INotifyCommands** ppNotifyCommands)
{
	return this->QueryInterface(IID_INotifyCommands, (void**)ppNotifyCommands);
}

